import 'package:flutter/material.dart';
import 'dart:math';

class RadioPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return RadioPageState();
  }
}

class RadioPageState extends State<RadioPage> {
  Color _randomColor() {
    var red = Random.secure().nextInt(255);
    var greed = Random.secure().nextInt(255);
    var blue = Random.secure().nextInt(255);
    return Color.fromARGB(255, red, greed, blue);
  }

  @override
  void initState() {
    super.initState();
  }

  int groupValue = 1;
  onChange(val) {
    this.setState(() {
      groupValue = val;
    });
  }

  String value = '';

  onChange2(v) {
    this.setState(() {
      value = v;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Radio Example"),
        ),
        body: ListView(
          children: <Widget>[
            Radio(
                value: 1,
                groupValue: groupValue, //当value和groupValue一致的时候则选中
                onChanged: (T) {
                  onChange(T);
                }),
            Radio(
                value: 2,
                groupValue: groupValue,
                onChanged: (T) {
                  onChange(T);
                }),
            Radio(
                value: 3,
                groupValue: groupValue,
                onChanged: (T) {
                  onChange(T);
                }),
            Divider(),
            RadioListTile<String>(
                title: const Text('A'),
                value: "A",
                groupValue: this.value,
                isThreeLine: false,
                subtitle: const Text("A的内容"),
                onChanged: onChange2),
            RadioListTile<String>(
                title: const Text('B'),
                value: "B",
                subtitle: const Text("B的内容"),
                groupValue: this.value,
                onChanged: onChange2),
          ],
        ));
  }
}
